package sk.naive.talker.command;

import sk.naive.talker.User;

import java.util.*;

/**
 * UserFindHelper.
 *
 * @author <a href="mailto:virgo@naive.deepblue.sk">Richard "Virgo" Richter</a>
 * @version $Revision: 1.3 $ $Date: 2005/01/08 10:39:22 $
 */
public class UserFindHelper {
	private Collection<User> users;
	private List<User> foundUsers;

	public UserFindHelper(Collection<User> users) {
		this.users = new HashSet<User>();
		this.users.addAll(users);
	}

	public void takeOut(Collection<User> out) {
		users.removeAll(out);
	}

	public void takeOut(User user) {
		users.remove(user);
	}

	/**
	 * Method performing find.
	 * Must be run before other testing methods are used.
	 *
	 * @param login must be normalized
	 * @return true, if the login is found
	 */
	public boolean find(String login) {
		foundUsers = new LinkedList<>();
		for (User user : users) {
			if (user.getLogin().equals(login)) {
				foundUsers.clear();
				foundUsers.add(user);
				break;
			}
			if (user.getLogin().startsWith(login)) {
				foundUsers.add(user);
			}
		}
		return isFound();
	}

	public List<User> allFound() {
		return foundUsers;
	}

	public User firstFound() {
		if (foundUsers.size() > 0) {
			return foundUsers.get(0);
		}
		return null;
	}

	public boolean isSecure() {
		return foundUsers.size() == 1;
	}

	public boolean isFound() {
		return foundUsers.size() > 0;
	}
}
